package gui;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.Model;
import model.NotifyPicassa;
import model.Pixmap;

/**
 * JFrame class that organizes and manages picassa pictures generated by model 
 * 
 * @author Troy Ferrell
 */
public class PictureFrame extends PicassaFrame implements  Observer,ComponentListener
{	
	public static final String NEW_IMAGE_STR = "New Image";
	
	private static final long serialVersionUID = 1L;
	
	private JTabbedPane myTabbedPane;
	private ArrayList<PicassaCanvas> myImages;
	private int currentImgIndex;
	
	public PictureFrame(String title, Model newModel)
	{
		this.setModel(newModel);
		this.setTitle(title);
		
		currentImgIndex = 0;
		myImages = new ArrayList<PicassaCanvas>();
		
		myTabbedPane = new JTabbedPane();
		addNewTab(NEW_IMAGE_STR);
		
		myTabbedPane.addChangeListener(new ChangeListener() 
		{
		    public void stateChanged(ChangeEvent evt) 
		    {
		    	currentImgIndex = myTabbedPane.getSelectedIndex();
		    	repaint();
		    }
		});
		
		this.getContentPane().add(myTabbedPane);
	}
	
	public void addNewTab(String imgName)
	{
		Dimension size = Pixmap.DEFAULT_SIZE;
		if(this.getWidth() > 0 && this.getHeight() > 0)
			size = new Dimension(this.getWidth(), this.getHeight());
		this.setPreferredSize(size);
		
		PicassaCanvas newCanvas = new PicassaCanvas(this, size);
		myImages.add(newCanvas);
		myTabbedPane.addTab(imgName, newCanvas);
		
		currentImgIndex = myImages.size() - 1;
		myTabbedPane.setTabComponentAt(currentImgIndex, new PictureTab(this, imgName));
		myTabbedPane.setSelectedIndex(currentImgIndex);
	}
	
	public void removeTab(PictureTab removeTab)
	{
		int index = myTabbedPane.indexOfTabComponent(removeTab);
		if(index < myImages.size() && index >= 0 && myImages.size() > 1)
		{
			myImages.remove(index);
			myTabbedPane.removeTabAt(index);
		}
		else
			System.out.println("ERROR!");
	}
	
	public void updateCurrentPicture(Pixmap newImg)
	{
		PicassaCanvas current = myImages.get(currentImgIndex);
		current.setPixmap(newImg);
		this.repaint();
	}
	
	public void repaint()
	{
		PicassaCanvas current = myImages.get(currentImgIndex);
		current.repaint();
		super.repaint();
	}
	
	private PicassaCanvas getCurrentCanvas()
	{
		if(currentImgIndex < 0 || currentImgIndex >= myImages.size() || myImages.isEmpty())
			return null;
		
		return myImages.get(currentImgIndex);
	}

	/**
	 *  Observer Interface(non-Javadoc)
	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
	 */
	public void update(Observable obs, Object obj)
	{
		//NotifyPicassa.showNotification("Picture updated....");
		
		myModel.evaluate((String)obj, new Dimension(this.getWidth(), this.getHeight()));
		
		PicassaCanvas current = this.getCurrentCanvas();
		current.setPixmap(myModel.currentImage);
		this.repaint();
	}
	
	// TODO: Listener doesn't seem to be responding
	
    public void componentHidden(ComponentEvent e) {}
    public void componentMoved(ComponentEvent e) {
    	System.out.println("HELLO");
    }
    public void componentShown(ComponentEvent e) {}
    public void componentResized(ComponentEvent e) 
    {
    	System.out.println(this.getWidth() + " - " + this.getHeight());
    }


}
